import os, sys
import os.path as osp
import numpy as np
import cv2
import json

from tool import filesystem
from paddlex import create_pipeline


pipeline = create_pipeline(pipeline="seal_recognition")


def seal_recognition_func(crop_image, debug=0):
    output = pipeline.predict(
        crop_image,
        use_doc_orientation_classify=False,
        use_doc_unwarping=False,
        use_layout_detection=False,
        layout_threshold=0.5,
        layout_nms=True,
        layout_unclip_ratio=1.0,
        layout_merge_bboxes_mode="large"
    )
    outputs = list(output)
    
    if debug:
        for res in outputs:
            # res.print() ## 打印预测的结构化输出
            res.save_to_img("./output/") ## 保存可视化结果
            res.save_to_json("./output/") ## 保存可视化结果
    
    ret_data = {}
    ret_data["rec_texts"] = []
    ret_data["rec_scores"] = []
    ret_data["rec_score"] = 0.0
    if len(outputs) == 0:
        return ret_data
    if len(outputs[0]["seal_res_list"]) == 0:
        return ret_data
        
    ret_data["rec_texts"] = outputs[0]["seal_res_list"][0]["rec_texts"]
    ret_data["rec_scores"] = outputs[0]["seal_res_list"][0]["rec_scores"]
    ret_data["rec_score"] = float(np.mean(ret_data["rec_scores"]))
    return ret_data


if __name__ == "__main__":
    file_path = sys.argv[1]
    file_paths = []
    if osp.isfile(file_path):
        file_paths.append(file_path)
    else:
        file_paths = filesystem.get_all_filepath(file_path, [".jpg", ".png"])
    
    for file_path in file_paths:
        bgr_img = cv2.imread(file_path)
        seal_recognition_func(bgr_img, debug=1)

"""
{
    "input_path": null,
    "page_index": null,
    "model_settings": {
        "use_doc_preprocessor": false,
        "use_layout_detection": false
    },
    "seal_res_list": [
        {
            "input_path": null,
            "page_index": null,
            "model_settings": {
                "use_doc_preprocessor": false,
                "use_textline_orientation": false
            },
            "dt_polys": [
                [
                    [
                        115,
                        7
                    ],
                    [
                        169,
                        28
                    ],
                    [
                        170,
                        29
                    ],
                    [
                        171,
                        31
                    ],
                    [
                        172,
                        32
                    ],
                    [
                        197,
                        89
                    ],
                    [
                        197,
                        90
                    ],
                    [
                        197,
                        93
                    ],
                    [
                        186,
                        144
                    ],
                    [
                        185,
                        146
                    ],
                    [
                        184,
                        147
                    ],
                    [
                        182,
                        148
                    ],
                    [
                        181,
                        149
                    ],
                    [
                        179,
                        149
                    ],
                    [
                        177,
                        149
                    ],
                    [
                        152,
                        139
                    ],
                    [
                        149,
                        137
                    ],
                    [
                        148,
                        135
                    ],
                    [
                        148,
                        133
                    ],
                    [
                        148,
                        131
                    ],
                    [
                        155,
                        96
                    ],
                    [
                        140,
                        64
                    ],
                    [
                        113,
                        48
                    ],
                    [
                        102,
                        47
                    ],
                    [
                        80,
                        51
                    ],
                    [
                        60,
                        68
                    ],
                    [
                        49,
                        102
                    ],
                    [
                        54,
                        132
                    ],
                    [
                        54,
                        133
                    ],
                    [
                        54,
                        135
                    ],
                    [
                        53,
                        137
                    ],
                    [
                        52,
                        138
                    ],
                    [
                        51,
                        138
                    ],
                    [
                        28,
                        148
                    ],
                    [
                        26,
                        149
                    ],
                    [
                        25,
                        149
                    ],
                    [
                        23,
                        148
                    ],
                    [
                        21,
                        147
                    ],
                    [
                        20,
                        146
                    ],
                    [
                        19,
                        144
                    ],
                    [
                        5,
                        96
                    ],
                    [
                        5,
                        94
                    ],
                    [
                        6,
                        91
                    ],
                    [
                        21,
                        56
                    ],
                    [
                        21,
                        55
                    ],
                    [
                        37,
                        28
                    ],
                    [
                        38,
                        27
                    ],
                    [
                        40,
                        25
                    ],
                    [
                        74,
                        13
                    ],
                    [
                        75,
                        13
                    ],
                    [
                        111,
                        7
                    ],
                    [
                        113,
                        7
                    ]
                ],
                [
                    [
                        140,
                        139
                    ],
                    [
                        141,
                        140
                    ],
                    [
                        143,
                        140
                    ],
                    [
                        144,
                        141
                    ],
                    [
                        144,
                        143
                    ],
                    [
                        145,
                        163
                    ],
                    [
                        145,
                        164
                    ],
                    [
                        144,
                        166
                    ],
                    [
                        143,
                        167
                    ],
                    [
                        141,
                        167
                    ],
                    [
                        134,
                        169
                    ],
                    [
                        134,
                        169
                    ],
                    [
                        100,
                        170
                    ],
                    [
                        76,
                        170
                    ],
                    [
                        61,
                        169
                    ],
                    [
                        60,
                        168
                    ],
                    [
                        58,
                        167
                    ],
                    [
                        58,
                        166
                    ],
                    [
                        57,
                        165
                    ],
                    [
                        57,
                        162
                    ],
                    [
                        57,
                        162
                    ],
                    [
                        57,
                        144
                    ],
                    [
                        57,
                        142
                    ],
                    [
                        58,
                        141
                    ],
                    [
                        60,
                        140
                    ],
                    [
                        61,
                        139
                    ],
                    [
                        121,
                        138
                    ]
                ],
                [
                    [
                        162,
                        155
                    ],
                    [
                        171,
                        162
                    ],
                    [
                        171,
                        163
                    ],
                    [
                        172,
                        164
                    ],
                    [
                        171,
                        166
                    ],
                    [
                        156,
                        178
                    ],
                    [
                        155,
                        178
                    ],
                    [
                        142,
                        186
                    ],
                    [
                        142,
                        186
                    ],
                    [
                        129,
                        190
                    ],
                    [
                        129,
                        190
                    ],
                    [
                        110,
                        194
                    ],
                    [
                        109,
                        194
                    ],
                    [
                        90,
                        194
                    ],
                    [
                        89,
                        194
                    ],
                    [
                        68,
                        190
                    ],
                    [
                        68,
                        189
                    ],
                    [
                        46,
                        178
                    ],
                    [
                        46,
                        177
                    ],
                    [
                        32,
                        166
                    ],
                    [
                        32,
                        165
                    ],
                    [
                        32,
                        164
                    ],
                    [
                        32,
                        162
                    ],
                    [
                        39,
                        155
                    ],
                    [
                        40,
                        155
                    ],
                    [
                        41,
                        154
                    ],
                    [
                        42,
                        155
                    ],
                    [
                        53,
                        164
                    ],
                    [
                        59,
                        167
                    ],
                    [
                        64,
                        171
                    ],
                    [
                        74,
                        175
                    ],
                    [
                        74,
                        175
                    ],
                    [
                        90,
                        178
                    ],
                    [
                        108,
                        179
                    ],
                    [
                        127,
                        175
                    ],
                    [
                        127,
                        175
                    ],
                    [
                        135,
                        172
                    ],
                    [
                        135,
                        172
                    ],
                    [
                        149,
                        163
                    ],
                    [
                        159,
                        155
                    ],
                    [
                        160,
                        154
                    ],
                    [
                        161,
                        154
                    ]
                ]
            ],
            "text_det_params": {
                "limit_side_len": 736,
                "limit_type": "min",
                "thresh": 0.2,
                "max_side_limit": 4000,
                "box_thresh": 0.6,
                "unclip_ratio": 0.5
            },
            "text_type": "seal",
            "textline_orientation_angles": [
                -1,
                -1,
                -1
            ],
            "text_rec_score_thresh": 0,
            "rec_texts": [
                "李尔长安重庆汽车系统有限公司",
                "财务专用章",
                "BASIPTIES9G0IJRRSH"
            ],
            "rec_scores": [
                0.9719530940055847,
                0.9997682571411133,
                0.8047266006469727
            ],
            "rec_polys": [
                [
                    [
                        115,
                        7
                    ],
                    [
                        169,
                        28
                    ],
                    [
                        170,
                        29
                    ],
                    [
                        171,
                        31
                    ],
                    [
                        172,
                        32
                    ],
                    [
                        197,
                        89
                    ],
                    [
                        197,
                        90
                    ],
                    [
                        197,
                        93
                    ],
                    [
                        186,
                        144
                    ],
                    [
                        185,
                        146
                    ],
                    [
                        184,
                        147
                    ],
                    [
                        182,
                        148
                    ],
                    [
                        181,
                        149
                    ],
                    [
                        179,
                        149
                    ],
                    [
                        177,
                        149
                    ],
                    [
                        152,
                        139
                    ],
                    [
                        149,
                        137
                    ],
                    [
                        148,
                        135
                    ],
                    [
                        148,
                        133
                    ],
                    [
                        148,
                        131
                    ],
                    [
                        155,
                        96
                    ],
                    [
                        140,
                        64
                    ],
                    [
                        113,
                        48
                    ],
                    [
                        102,
                        47
                    ],
                    [
                        80,
                        51
                    ],
                    [
                        60,
                        68
                    ],
                    [
                        49,
                        102
                    ],
                    [
                        54,
                        132
                    ],
                    [
                        54,
                        133
                    ],
                    [
                        54,
                        135
                    ],
                    [
                        53,
                        137
                    ],
                    [
                        52,
                        138
                    ],
                    [
                        51,
                        138
                    ],
                    [
                        28,
                        148
                    ],
                    [
                        26,
                        149
                    ],
                    [
                        25,
                        149
                    ],
                    [
                        23,
                        148
                    ],
                    [
                        21,
                        147
                    ],
                    [
                        20,
                        146
                    ],
                    [
                        19,
                        144
                    ],
                    [
                        5,
                        96
                    ],
                    [
                        5,
                        94
                    ],
                    [
                        6,
                        91
                    ],
                    [
                        21,
                        56
                    ],
                    [
                        21,
                        55
                    ],
                    [
                        37,
                        28
                    ],
                    [
                        38,
                        27
                    ],
                    [
                        40,
                        25
                    ],
                    [
                        74,
                        13
                    ],
                    [
                        75,
                        13
                    ],
                    [
                        111,
                        7
                    ],
                    [
                        113,
                        7
                    ]
                ],
                [
                    [
                        140,
                        139
                    ],
                    [
                        141,
                        140
                    ],
                    [
                        143,
                        140
                    ],
                    [
                        144,
                        141
                    ],
                    [
                        144,
                        143
                    ],
                    [
                        145,
                        163
                    ],
                    [
                        145,
                        164
                    ],
                    [
                        144,
                        166
                    ],
                    [
                        143,
                        167
                    ],
                    [
                        141,
                        167
                    ],
                    [
                        134,
                        169
                    ],
                    [
                        134,
                        169
                    ],
                    [
                        100,
                        170
                    ],
                    [
                        76,
                        170
                    ],
                    [
                        61,
                        169
                    ],
                    [
                        60,
                        168
                    ],
                    [
                        58,
                        167
                    ],
                    [
                        58,
                        166
                    ],
                    [
                        57,
                        165
                    ],
                    [
                        57,
                        162
                    ],
                    [
                        57,
                        162
                    ],
                    [
                        57,
                        144
                    ],
                    [
                        57,
                        142
                    ],
                    [
                        58,
                        141
                    ],
                    [
                        60,
                        140
                    ],
                    [
                        61,
                        139
                    ],
                    [
                        121,
                        138
                    ]
                ],
                [
                    [
                        162,
                        155
                    ],
                    [
                        171,
                        162
                    ],
                    [
                        171,
                        163
                    ],
                    [
                        172,
                        164
                    ],
                    [
                        171,
                        166
                    ],
                    [
                        156,
                        178
                    ],
                    [
                        155,
                        178
                    ],
                    [
                        142,
                        186
                    ],
                    [
                        142,
                        186
                    ],
                    [
                        129,
                        190
                    ],
                    [
                        129,
                        190
                    ],
                    [
                        110,
                        194
                    ],
                    [
                        109,
                        194
                    ],
                    [
                        90,
                        194
                    ],
                    [
                        89,
                        194
                    ],
                    [
                        68,
                        190
                    ],
                    [
                        68,
                        189
                    ],
                    [
                        46,
                        178
                    ],
                    [
                        46,
                        177
                    ],
                    [
                        32,
                        166
                    ],
                    [
                        32,
                        165
                    ],
                    [
                        32,
                        164
                    ],
                    [
                        32,
                        162
                    ],
                    [
                        39,
                        155
                    ],
                    [
                        40,
                        155
                    ],
                    [
                        41,
                        154
                    ],
                    [
                        42,
                        155
                    ],
                    [
                        53,
                        164
                    ],
                    [
                        59,
                        167
                    ],
                    [
                        64,
                        171
                    ],
                    [
                        74,
                        175
                    ],
                    [
                        74,
                        175
                    ],
                    [
                        90,
                        178
                    ],
                    [
                        108,
                        179
                    ],
                    [
                        127,
                        175
                    ],
                    [
                        127,
                        175
                    ],
                    [
                        135,
                        172
                    ],
                    [
                        135,
                        172
                    ],
                    [
                        149,
                        163
                    ],
                    [
                        159,
                        155
                    ],
                    [
                        160,
                        154
                    ],
                    [
                        161,
                        154
                    ]
                ]
            ],
            "rec_boxes": []
        }
    ]
}    
"""
